//
//  PKReadCommentTableViewController.m
//  PianKe
//
//  Created by 王郑 on 16/1/28.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "PKReadCommentTableViewController.h"
#import "PKReadCommentTableViewCell.h"
#import "PKReadCommentModel.h"

@interface PKReadCommentTableViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (strong, nonatomic) UITableView *tableview;
// 请求的初始位置
@property (assign, nonatomic) NSInteger requestStart;

// 数据源数组
@property (strong, nonatomic) NSMutableArray *listArray;


@end

@implementation PKReadCommentTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
#pragma mark ----  添加表示图:
    self.view.backgroundColor = [UIColor whiteColor];
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
#pragma mark ---- 数据源信息
    _listArray = [NSMutableArray array];
    _requestStart = 0;
    
#pragma mark ---- 添加自定义导航栏
    [self addNavBar];
    
    UINib *cellNib = [UINib nibWithNibName:@"PKReadCommentTableViewCell" bundle:nil];
    [_tableview registerNib:cellNib
         forCellReuseIdentifier:@"CommentCell"];
    _tableview.rowHeight = UITableViewAutomaticDimension;
    _tableview.estimatedRowHeight = 10;
#pragma mark ---- 解析数据
    [self setUpCommentData];


#pragma mark  ----- 上啦加载
    _tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _requestStart += 10;
        
        [self setUpCommentData];
        
    }];
    
#pragma mark ----- 下拉刷新
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _requestStart = 0;
        
        [self setUpCommentData];
        
    }];
    
    


}
- (void)addNavBar
{
    
    UIView *barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    barView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:barView];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10 * kWidthBase, 30 * kHeightBase, 20 * kWidthBase, 20 * kHeightBase);
    [backButton setBackgroundImage:[UIImage imageNamed:@"info-back"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [barView addSubview:backButton];
    
    
    
}

- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setUpCommentData
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *auth = [user objectForKey:@"auth"];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = auth;
    parDic[@"client"] = @"1";
    parDic[@"contentid"] = _contentid;
    parDic[@"version"] = @"3.0.6";
    parDic[@"deviceid"] = @"F7EFB566-54F1-4912-ADFC-7E788F6D4D00";
    parDic[@"limit"] = @"10";
    parDic[@"start"] = [NSString stringWithFormat:@"%ld",_requestStart];
    
    [PKRequestManager requestWithUrlString:_commentUrl parDic:parDic Method:POST finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *list = dataDic[@"list"];
        for (NSDictionary *oneDic in list) {
            
            
            
        }
        
        
        
    } error:^(NSError *error) {
       
        NSLog(@"%@",error);
        
    }];
    
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

  //  return _listArray.count;
    return 20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKReadCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell" forIndexPath:indexPath];
    
    
    return cell;
}



@end
